package com.petdog.common.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.file.Paths;

/**
 * Web配置类
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {
    
    @Value("${file.upload-dir}")
    private String uploadDir;
    
    @Value("${file.static-path}")
    private String staticPath;
    
    /**
     * 配置静态资源访问路径
     * 允许前端通过URL访问上传的媒体文件
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 将/upload/**路径映射到实际的文件存储目录
        String resourcePath = Paths.get(uploadDir).toUri().toString();
        registry.addResourceHandler(staticPath + "/**")
                .addResourceLocations(resourcePath);
    }
}